<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Monhoc extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {
		//parent::Controller();
		parent::__construct(); //for CI 2.x users 
		$this->load->module_model('member', 'monhoc_model', 'monhoc');
		$this->load->helper('text');
		$this->user = $this->access->get_user();
		$this->load->helper('url');
		$this->access->restrict();
	}
	
	public function index()
	{
		//$this->access->restrict('Member.Monhoc.Index');
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách Môn học';
		$data['description'] = 'Quản lý danh sách Môn học';
		
		$this->access->restrict();
		$this->load->library('basic_library');
		$this->load->library('pagination');
		
		$config = array();
		$config['base_url'] = base_url().'member/monhoc/index';
		$config['total_rows'] = $this->monhoc->count_all_monhoc();
		$config['per_page'] = 100;
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		$config['first_link'] = 'First';
		$config['last_link'] = 'Last';
		$config['next_link'] = 'Next';
		$config['prev_link'] = 'Prev';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		$data['sotrang']	=	$this->uri->segment(4);
		$data['allmonhoc'] = $this->monhoc->get_all_monhoc($config['per_page'], $page);
		$data['links'] = $this->pagination->create_links();
		
		$this->_template['page']	= 'users/monhoc/list_monhoc';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function edit_monhoc($id)
	{
		//$this->access->restrict('Member.Monhoc.Edit');
		$data['ItemID'] = 'User';
		$data['title'] = 'Sửa thông tin Môn học';
		$data['description'] = 'Sửa thông tin Môn học';
		
		$this->load->helper('form');
		$data['info'] = $this->monhoc->get_monhoc_info_by_id($id);
		$data['allkhoa'] = $this->monhoc->get_all_khoa();
		$userid = $this->user->id;
		$lang_working = load_language_working();
		//here is validation form
		$this->form_validation->set_rules('tenmon', 'Tên môn học', 'required|callback_check_code');
		$this->form_validation->set_rules('sotinchi','Số tín chỉ','required');
		
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if ($this->form_validation->run() == TRUE)
		{
			$this->monhoc->edit_monhoc($id);
			$this->session->set_flashdata('message', 'Cập nhật thành công');
			redirect('member/monhoc/', 'refresh');
		}
		$this->_template['page']	= 'users/monhoc/edit_monhoc';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function add_monhoc()
	{
		//$this->access->restrict('Member.Monhoc.Edit');
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm mới Môn học';
		$data['description'] = 'Thêm mới Môn học';
		
		$this->load->helper('form');
		$data['allkhoa'] = $this->monhoc->get_all_khoa();
		$userid = $this->user->id;
		$lang_working = load_language_working();
		//here is validation form
		$this->form_validation->set_rules('tenmon', 'Tên môn học', 'required|callback_check_code');
		$this->form_validation->set_rules('sotinchi','Số tín chỉ','required');
		
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if ($this->form_validation->run() == TRUE)
		{
			$this->monhoc->add_monhoc();
			$this->session->set_flashdata('message', 'Thêm mới thành công');
			redirect('member/monhoc/', 'refresh');
		}
		$this->_template['page']	= 'users/monhoc/add_monhoc';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function search()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Tìm kiếm thông tin';
		$data['description'] = 'Tìm kiếm thông tin';
		
		$data['keyword'] = $this->input->post('keyword', true);
		$data['results'] = $this->monhoc->search_subject($data['keyword']);
		
		$this->_template['page']	= 'users/monhoc/search';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function monhoc_exchange()
	{
		$id = $this->input->post('id');
		if($id !==null)
		{
			$data = $this->monhoc->get_monhoc_info_by_id($id);
			$status = 0;
			if($data -> status=='0')
			{
				$status = 1;
			}
			$this->monhoc->monhoc_exchange($id, $status);
			$row['result'] = 1;
			$row['status'] = $status;
			echo json_encode($row);
		}
		else
		{
			$row['result'] = 0;
			echo json_encode($row);
		}
	}
}

/* Location: ./application/modules/member/controllers/monhoc.php */